// pages/toDolist/toDolist.js
Page({
  data: {
    value:[
      {name:"zz",state:false},
      {name:"psc",state:true},
    ]
  },
  onLoad: function (options) {
    var lists = wx.getStorageSync('lists')
    if(lists){
      this.setData({
        value:lists
      })
    }else{
      wx.setStorageSync('lists', [])
      
    }
  },
  onInput(event){
    let value = this.data.value;
    var detail = event.detail.trim();
    var res = value.every( res=>res.name != detail);
    if(detail && res){
      value.push({
        name:detail,
        state:false
      })
    }else{
      wx.showToast({
        title: '数据已存在',
        icon: ''
      })
    }
    wx.setStorageSync('lists', value)
    this.setData({
      value
    })
  },
  onChange(event){
    this.data.value.forEach((res,index)=>{
      if(index == event.currentTarget.dataset.id){
        res.state = event.detail
      }
    })
    wx.setStorageSync('lists', this.data.value)
    this.setData({
      value:this.data.value
    })
  },
  onClick(event){
    this.data.value.forEach((res,index)=>{
      if(index == event.currentTarget.dataset.id){
        this.data.value.splice(index,1)
      }
    })
    wx.setStorageSync('lists', this.data.value)
    this.setData({
      value:this.data.value
    })
  }
})